package com.ljl.ssm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ljl.ssm.pojo.Collection;
import com.ljl.ssm.entity.Product;
import com.ljl.ssm.mapper.CollectionMapper;
import com.ljl.ssm.mapper.ProductMapper;
import com.ljl.ssm.service.ICollectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author BaoZhou
 * @since 2019-07-07
 */
@Service
public class CollectionServiceImpl extends ServiceImpl<CollectionMapper, Collection> implements ICollectionService {

    @Autowired
    CollectionMapper collectionMapper;
    @Autowired
    ProductMapper productMapper;

    @Override
    public int insert(Collection collection) {
        return collectionMapper.insert(collection);
    }
    @Override
    public int delete(int userid,int productid){
        QueryWrapper<Collection> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_id",userid).eq("product_id",productid);
        return collectionMapper.delete(queryWrapper);
    }
    @Override
    public int select(int userid,int productid){
        int result;
        QueryWrapper<Collection> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_id",userid).eq("product_id",productid);
        result=collectionMapper.selectCount(queryWrapper);
        return result;

    }

    @Override
    public PageInfo selectAll(int user_id ,int pn) {

        QueryWrapper<Collection> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_id",user_id);
        List<Collection> collectionList=collectionMapper.selectList(queryWrapper);

        QueryWrapper<Product> productQueryWrapper=new QueryWrapper<>();

        for (Collection  collection:collectionList
        ) {
            productQueryWrapper.eq("id",collection.getProductId());
            productQueryWrapper.or();
        }

        if (collectionList.size()==0){

            return  new PageInfo();
        }

        PageHelper.startPage(pn,3);
        List<Product> productList=productMapper.selectList(productQueryWrapper);
        PageInfo pageInfo = new PageInfo<>(productList);

        return pageInfo;
    }
}
